package me.zhengjie.ship.shipperbase.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.ship.shipperbase.domain.ShipPerBase;
import me.zhengjie.ship.shipperbase.service.ShipPerBaseService;
import me.zhengjie.ship.shipperbase.service.dto.ShipPerBaseQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author liwh
* @date 2020-04-22
*/
@Api(tags = "舰艇人员信息管理")
@RestController
@RequestMapping("/api/shipPerBase")
public class ShipPerBaseController {

    private final ShipPerBaseService shipPerBaseService;

    public ShipPerBaseController(ShipPerBaseService shipPerBaseService) {
        this.shipPerBaseService = shipPerBaseService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('shipPerBase:list')")
    public void download(HttpServletResponse response, ShipPerBaseQueryCriteria criteria) throws IOException {
        shipPerBaseService.download(shipPerBaseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询舰艇人员信息")
    @ApiOperation("查询舰艇人员信息")
    @PreAuthorize("@el.check('shipPerBase:list')")
    public ResponseEntity<Object> getShipPerBases(ShipPerBaseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(shipPerBaseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增舰艇人员信息")
    @ApiOperation("新增舰艇人员信息")
    @PreAuthorize("@el.check('shipPerBase:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ShipPerBase resources){
        return new ResponseEntity<>(shipPerBaseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改舰艇人员信息")
    @ApiOperation("修改舰艇人员信息")
    @PreAuthorize("@el.check('shipPerBase:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ShipPerBase resources){
        shipPerBaseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除舰艇人员信息")
    @ApiOperation("删除舰艇人员信息")
    @PreAuthorize("@el.check('shipPerBase:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        shipPerBaseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}